package com.medicalsystem.controller;

import com.medicalsystem.entity.Appointment;
import com.medicalsystem.service.AppointmentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/appointments")
@Tag(name = "预约挂号管理", description = "提供预约挂号的增删改查功能")
public class AppointmentController {

    private final AppointmentService appointmentService;

    public AppointmentController(AppointmentService appointmentService) {
        this.appointmentService = appointmentService;
    }

    @Operation(summary = "获取所有预约信息", description = "返回所有预约挂号信息列表")
    @GetMapping
    public List<Appointment> getAllAppointments() {
        return appointmentService.getAllAppointments();
    }

    @Operation(summary = "根据ID获取预约信息", description = "通过预约ID获取预约详细信息")
    @GetMapping("/{id}")
    public ResponseEntity<Appointment> getAppointmentById(@PathVariable Long id) {
        Appointment appointment = appointmentService.getAppointmentById(id);
        return appointment != null ? ResponseEntity.ok(appointment) : ResponseEntity.notFound().build();
    }

    @Operation(summary = "新增预约信息", description = "保存预约挂号信息到数据库")
    @PostMapping
    public Appointment saveAppointment(@RequestBody Appointment appointment) {
        appointment.setCreateTime(new java.util.Date());
        appointment.setUpdateTime(new java.util.Date());
        return appointmentService.saveAppointment(appointment);
    }

    @Operation(summary = "修改预约信息", description = "根据ID修改预约详细信息")
    @PutMapping("/{id}")
    public ResponseEntity<Appointment> updateAppointment(@PathVariable Long id, @RequestBody Appointment appointment) {
        Appointment updatedAppointment = appointmentService.updateAppointment(id, appointment);
        return updatedAppointment != null ? ResponseEntity.ok(updatedAppointment) : ResponseEntity.notFound().build();
    }

    @Operation(summary = "删除预约信息", description = "根据ID删除预约挂号信息")
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteAppointment(@PathVariable Long id) {
        appointmentService.deleteAppointment(id);
        return ResponseEntity.noContent().build();
    }
}
